package com.example.studentinfo;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Find extends AppCompatActivity {
    EditText find;
    LinearLayout xb,cj;
    private Spinner spin_one;
    Button btn_find;
    int flag;
    DbHelper dbHelper;
    ListView listView;
    Cursor cursor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find);
        setTitle("QueryStudentInfo");
        find=findViewById(R.id.text_find);
        xb=findViewById(R.id.ly_xb);
        cj=findViewById(R.id.ly_cj);
        listView=findViewById(R.id.list_find);
        dbHelper = new DbHelper(Find.this,"MyDataBase,",2);
        setlistener();

    }

    public void setlistener(){
        spin_one=findViewById(R.id.spin_one);
        spin_one.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                flag=i;
                switch (i){
                    case 0:
                        find.setVisibility(View.VISIBLE);
                        xb.setVisibility(View.GONE);
                        cj.setVisibility(View.GONE);
                        find.setHint("请输入姓名，支持模糊查询");
                        break;
                    case 1:
                        find.setVisibility(View.GONE);
                        xb.setVisibility(View.VISIBLE);
                        cj.setVisibility(View.GONE);
                        break;
                    case 2:
                        find.setVisibility(View.VISIBLE);
                        xb.setVisibility(View.GONE);
                        cj.setVisibility(View.GONE);
                        find.setHint("请输入学号，支持模糊查询");
                        break;
                    case 3:
                        find.setVisibility(View.VISIBLE);
                        xb.setVisibility(View.GONE);
                        cj.setVisibility(View.GONE);
                        find.setHint("请输入班级");
                        break;
                    case 4:
                        find.setVisibility(View.VISIBLE);
                        xb.setVisibility(View.GONE);
                        cj.setVisibility(View.GONE);
                        find.setHint("请输入专业，支持模糊查询");
                        break;
                    case 5:
                        find.setVisibility(View.GONE);
                        xb.setVisibility(View.GONE);
                        cj.setVisibility(View.VISIBLE);
                        break;

                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        btn_find=findViewById(R.id.btn_find);
        btn_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                find();
                String from[] = new String[]{"xm", "xb", "xh","bj","zy","cj"};
                int to[] = new int[]{R.id.xm,R.id.xb, R.id.xh, R.id.bj, R.id.zy, R.id.cj};
                /**
                 * SimpleCursorAdapter允许你绑定一个游标的列到ListView上。
                 * 需要传入当前的上下文、一个layout文件，一个游标
                 * 和两个数组：一个包含使用的游标集中列的名字，另一个(相同大小)数组包含View中的资源ID，用于显示相应列的数据值。
                 */
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(Find.this, R.layout.listview, cursor, from, to, 0);
                listView.setAdapter(adapter);
            }
        });
    }
    public void findbyname(String q){
        cursor =  dbHelper.queryByStudentXm("Student",q);
    }
    public void findbysex(String q){
        cursor =  dbHelper.queryByStudentXb("Student",q);
    }
    public void findbynum(String q){
        cursor =  dbHelper.queryByStudentXh("Student",q);
    }
    public void findbyclass(String q){
        cursor =  dbHelper.queryByStudentBj("Student",q);
    }
    public void findbypro(String q){
        cursor =  dbHelper.queryByStudentZy("Student",q);
    }
    public void findbygrade(String min, String max){
        cursor =  dbHelper.queryByStudentCj("Student",min,max);
    }

    public void find(){
        String findin=find.getText().toString();
        //获取性别
        RadioGroup radgroup = findViewById(R.id.radioGroup);
        RadioButton radbtn=findViewById(radgroup.getCheckedRadioButtonId()) ;
        String xb;
        if (radgroup.getCheckedRadioButtonId()==R.id.btnMan){
            xb="男";
        }
        else{
            xb="女";
        }
        //获取分数区间
       EditText min= findViewById(R.id.tx_min);
       EditText max= findViewById(R.id.tx_max);
       String smin=min.getText().toString();
       String smax=max.getText().toString();
        switch (flag){
            case 0:findbyname(findin);break;
            case 1:findbysex(xb);break;
            case 2:findbynum(findin);break;
            case 3:findbyclass(findin);break;
            case 4:findbypro(findin);break;
            case 5:findbygrade(smin,smax);break;
        }
    }
}